package com.ht.modules.system.online.codegenerate.a;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

@Slf4j
public class ConfigVo {
    public String schemaName;
    public String diverName;
    public String url;
    public String username;
    public String password;
    public String projectPath;
    public String bussiPackage;
    public String sourceRootPackage;
    public String webRootPackage;
    @Setter
    public String templatePath;
    public boolean dbFiledConvert;
    public String primaryKeyField;
    public int fieldRequiredNum;
    public int pageSearchFiledNum;
    public String pageFilterFields;
    public int fieldRowNum;

    public ConfigVo() {
        ResourceBundle database = this.readConfigurationFile("/jeecg/database.properties");
        ResourceBundle config = this.readConfigurationFile("/jeecg/config.properties");
        schemaName = "public";
        diverName = "com.mysql.jdbc.Driver";
        url = "jdbc:mysql://localhost:3306/jeecg-boot?useUnicode=true&characterEncoding=UTF-8";
        username = "root";
        password = "root";
        projectPath = "c:/workspace/jeecg";
        bussiPackage = "com.jeecg";
        sourceRootPackage = "src";
        webRootPackage = "WebRoot";
        templatePath = "/jeecg/code-template/";
        dbFiledConvert = true;
        fieldRequiredNum = 4;
        pageSearchFiledNum = 3;
        fieldRowNum = 1;
        diverName = database.getString("diver_name");
        url = database.getString("url");
        schemaName = database.getString("database_name");
        username = database.getString("username");
        password = database.getString("password");
        sourceRootPackage = config.getString("source_root_package");
        webRootPackage = config.getString("webroot_package");
        bussiPackage = config.getString("bussi_package");
        templatePath = config.getString("templatepath");
        projectPath = config.getString("project_path");
        primaryKeyField = config.getString("db_table_id");
        dbFiledConvert = Boolean.parseBoolean(config.getString("db_filed_convert"));
        pageFilterFields = config.getString("page_filter_fields");
        pageSearchFiledNum = Integer.parseInt(config.getString("page_search_filed_num"));
        sourceRootPackage = sourceRootPackage.replace(".", "/");
        webRootPackage = webRootPackage.replace(".", "/");
    }

    /**
     * 读取配置文件
     *
     * @param fileName 配置文件名称
     * @return ResourceBundle
     */
    private ResourceBundle readConfigurationFile(String fileName) {
        try (InputStream inputStream = new ClassPathResource(fileName ).getInputStream()) {
            return new PropertyResourceBundle(new BufferedInputStream(inputStream));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
